package com.example.mall_system.controller;

import com.example.mall_system.common.Result;
import com.example.mall_system.entity.Address;
import com.example.mall_system.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/address")
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;

    // 获取用户所有地址
    @GetMapping
    public Result<?> getAllAddresses() {
        List<Address> addresses = addressMapper.selectAll();
        return Result.success(addresses);
    }

    // 获取用户默认地址
    @GetMapping("/default")
    public Result<?> getDefaultAddress(@RequestParam Long userId) {
        Address defaultAddress = addressMapper.selectDefaultByUserId(userId);
        if (defaultAddress == null) {
            // 如果没有默认地址，返回第一个地址
            List<Address> addresses = addressMapper.selectByUserId(userId);
            if (!addresses.isEmpty()) {
                defaultAddress = addresses.get(0);
            } else {
                // 如果用户没有任何地址，返回错误信息
                return Result.error(404, "用户没有收货地址");
            }
        }
        return Result.success(defaultAddress);
    }

    // 添加新地址
    @PostMapping
    public Result<?> addAddress(@RequestBody Address address) {
        address.setCreateTime(new Date());
        address.setUpdateTime(new Date());
        
        // 如果设置为默认地址，先取消其他默认地址
        if (Boolean.TRUE.equals(address.getIsDefault())) {
            addressMapper.setDefault(null, address.getUserId());
        }
        
        int result = addressMapper.insert(address);
        if (result > 0) {
            return Result.success("地址添加成功");
        } else {
            return Result.error("地址添加失败");
        }
    }

    // 更新地址
    @PutMapping("/{id}")
    public Result<?> updateAddress(
            @PathVariable Long id,
            @RequestBody Address address,
            @RequestParam Long userId) {
        
        // 验证地址是否属于该用户
        Address existingAddress = addressMapper.selectByIdAndUserId(id, userId);
        if (existingAddress == null) {
            return Result.error("地址不存在或无权限修改");
        }
        
        address.setId(id);
        address.setUserId(userId);
        address.setUpdateTime(new Date());
        
        // 如果设置为默认地址，先取消其他默认地址
        if (Boolean.TRUE.equals(address.getIsDefault())) {
            addressMapper.setDefault(null, userId);
        }
        
        int result = addressMapper.update(address);
        if (result > 0) {
            return Result.success("地址更新成功");
        } else {
            return Result.error("地址更新失败");
        }
    }

    // 删除地址
    @DeleteMapping("/{id}")
    public Result<?> deleteAddress(
            @PathVariable Long id,
            @RequestParam Long userId) {
        
        int result = addressMapper.deleteById(id, userId);
        if (result > 0) {
            return Result.success("地址删除成功");
        } else {
            return Result.error("地址删除失败");
        }
    }

    // 设置默认地址
    @PostMapping("/{id}/default")
    public Result<?> setDefaultAddress(
            @PathVariable Long id,
            @RequestParam Long userId) {
        
        int result = addressMapper.setDefault(id, userId);
        if (result > 0) {
            return Result.success("默认地址设置成功");
        } else {
            return Result.error("默认地址设置失败");
        }
    }

    // 获取地址详情
    @GetMapping("/{id}")
    public Result<?> getAddressDetail(
            @PathVariable Long id,
            @RequestParam Long userId) {
        
        Address address = addressMapper.selectByIdAndUserId(id, userId);
        if (address != null) {
            return Result.success(address);
        } else {
            return Result.error("地址不存在");
        }
    }
} 